<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.time.temporal.ChronoUnit" %>
<%@ page import="java.time.LocalDateTime" %>
<%@ page import="java.sql.Timestamp" %>
<%@ page import="com.wy.util.DBUtil" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.concurrent.TimeUnit" %>
<%@ page import="java.util.Date" %>
<%--
  Created by IntelliJ IDEA.
  User: 13939
  Date: 2024/11/30
  Time: 11:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <%
        //检测用户是否已经登陆了
        String username  = (String) session.getAttribute("username");

        if(username==null){
            response.sendRedirect("/login/login.jsp");
            return;  //!!!!!不能少  返回  不执行下面的语句
        }
    %>
    <meta charset="UTF-8">
    <title>文章详情</title>
    <link rel="stylesheet" href="../styles/newShow.css">
</head>
<body>
<%
    // 获取传递的 newsId 参数
    String newsIdStr = request.getParameter("hid");
    int newsId = Integer.parseInt(newsIdStr);
    String sql = "select * from news_headline where hid = " + newsId;

    DBUtil dbUtil=new DBUtil();
    ResultSet rs=dbUtil.query(sql);
    rs.next();
    String title=rs.getString("title");

    int publisher = rs.getInt("publisher");
    String sqlPublisher="select * from news_user where uid = "+publisher;
    ResultSet rs1 = dbUtil.query(sqlPublisher);
    rs1.next();


    String nick_name = rs1.getString("nick_name");
    String article=rs.getString("article");

    int type = rs.getInt("type");
    String sqlType="select tname from news_type where tid = "+type;
    ResultSet rs2 = dbUtil.query(sqlType);
    rs2.next();
    String typeName = rs2.getString("tname");


    String updateTimeStr = rs.getString("update_time");

    // 定义日期格式，假设数据库中的日期格式为 "yyyy-MM-dd HH:mm:ss"
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    // 将字符串转换为 Date 对象
    Date updateTime = dateFormat.parse(updateTimeStr);
    Date now = new Date();

    // 计算时间差（毫秒）
    long diffInMillis = now.getTime() - updateTime.getTime();

    // 将毫秒转换为小时
    long diffInHours = TimeUnit.MILLISECONDS.toHours(diffInMillis);


    int pageViews = rs.getInt("page_views");
    String updateSql = "UPDATE news_headline SET page_views = page_views + 1 WHERE hid = "+newsId;
    int update = dbUtil.update(updateSql);

%>
<div class="container">

    <h1><%= title %></h1>
    <div class="article-content">
        <p><%= article %></p>
    </div>
    <div class="article-info">
        <p>作者: <%= nick_name %></p>
        <p>类别: <%= typeName %></p>
        <p>发布时间: <%= diffInHours %> 小时前</p>
    </div>

    <div class="mt-4">
        <a href="/show/show.jsp" class="btn btn-primary">返回主页</a>
    </div>
</div>

</body>
</html>
